package dgopenai

import (
	"net/http"
	"time"

	dghttp "github.com/darwinOrg/go-httpclient"
	"github.com/sashabaranov/go-openai"
)

var platformClientMap map[string]*openai.Client

// PlatformClientConfig holds the configuration for a platform client
type PlatformClientConfig struct {
	Platform  string `json:"platform" remark:"平台"`
	BaseUrl   string `json:"baseUrl" remark:"基本url"`
	AuthToken string `json:"authToken" remark:"认证token"`
}

// InitPlatformClients initializes the platform clients with the given configurations
func InitPlatformClients(timeout time.Duration, clientConfigs ...*PlatformClientConfig) {
	httpClient := &http.Client{
		Transport: dghttp.HttpTransport,
		Timeout:   timeout,
	}

	platformClientMap = make(map[string]*openai.Client, len(clientConfigs))

	for _, clientConfig := range clientConfigs {
		cfg := openai.DefaultConfig(clientConfig.AuthToken)
		cfg.BaseURL = clientConfig.BaseUrl
		cfg.HTTPClient = httpClient
		platformClientMap[clientConfig.Platform] = openai.NewClientWithConfig(cfg)
	}
}
